package com.qiantu.phone.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;

import com.hjq.http.listener.HttpCallback;
import com.hjq.shape.layout.ShapeLinearLayout;
import com.hjq.widget.layout.SettingBar;
import com.qiantu.api.db.LLControllerDBManager;
import com.qiantu.api.entity.ControllerBean;
import com.qiantu.api.entity.DeviceBean;
import com.qiantu.api.entity.KGXcollectionData;
import com.qiantu.api.entity.PanelBindConfig;
import com.qiantu.api.entity.PanelBindConfigs;
import com.qiantu.api.entity.SceneBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.aop.Log;
import com.qiantu.phone.app.AppActivity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class KGXPanelBindConfigActivity extends AppActivity {
    private int SCENE_BIND_COUNT = 12;
    private int LIGHT_BIND_COUNT = 64;
    private int CURTAIN_BIND_COUNT = 8;
    private int AIR_CONDITIONER_COUNT = 8;
    private int FLOOR_HEATING_BIND_COUNT = 8;
    private int FRESH_AIR_BIND_COUNT = 8;
    private List<PanelBindConfig> mSceneConfigData = new ArrayList<>();
    private List<PanelBindConfig> mLightConfigData = new ArrayList<>();
    private List<PanelBindConfig> mCurtainConfigData = new ArrayList<>();
    private List<PanelBindConfig> mAirConditionerConfigData = new ArrayList<>();
    private List<PanelBindConfig> mFloorHeatingConfigData = new ArrayList<>();
    private List<PanelBindConfig> mFreshAirConfigData = new ArrayList<>();
    private ShapeLinearLayout mSceneLayout;
    private ShapeLinearLayout mLightLayout;
    private ShapeLinearLayout mCurtainLayout;
    private SettingBar mSceneConfig;
    private SettingBar mLightConfig;
    private SettingBar mCurtainConfig;
    private SettingBar mAirConditionerType;
    private SettingBar mFloorHeatingType;
    private SettingBar mFreshAirConfig;
    private String productModel;
    private KGXcollectionData deviceCollectBean;
    @Log
    public static void start(Context context, KGXcollectionData deviceCollectBean) {
        Intent intent = new Intent(context, KGXPanelBindConfigActivity.class);
        intent.putExtra("deviceCollectBean", deviceCollectBean);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_kgxpanel_bind_config;
    }

    @Override
    public void initView() {

        deviceCollectBean = getSerializable("deviceCollectBean");
        mSceneLayout = findViewById(R.id.scene_config_shape_layout);
        mSceneConfig = findViewById(R.id.scene_config);
        mLightLayout = findViewById(R.id.light_config_shape_layout);
        mLightConfig = findViewById(R.id.light_config);
        mCurtainLayout = findViewById(R.id.curtain_config_shape_layout);
        mCurtainConfig = findViewById(R.id.curtain_config);
        mAirConditionerType = findViewById(R.id.air_conditioner_type);
        mFloorHeatingType = findViewById(R.id.floor_heating_type);
        mFreshAirConfig = findViewById(R.id.fresh_air_config);
        setOnClickListener(mSceneConfig, mLightConfig, mCurtainConfig, mAirConditionerType, mFreshAirConfig, mFloorHeatingType);

        mSceneLayout.setVisibility(View.GONE);
        mLightLayout.setVisibility(View.GONE);
        mCurtainLayout.setVisibility(View.GONE);
    }

    @Override
    protected void initData() {
        AIR_CONDITIONER_COUNT = 1;
        FRESH_AIR_BIND_COUNT = 1;
        FLOOR_HEATING_BIND_COUNT = 1;
    }

    @Override
    protected void onResume() {
        super.onResume();
        LLHttpManager.getPanelBindConfig(getAppActivity(), deviceCollectBean.getControllerSerialNo(), new HttpCallback<HttpData<PanelBindConfigs>>(getAppActivity()){
            @Override
            public void onSucceed(HttpData<PanelBindConfigs> result) {
                super.onSucceed(result);
                mLightConfigData = new ArrayList<>();
                mAirConditionerConfigData = new ArrayList<>();
                mFreshAirConfigData = new ArrayList<>();
                mCurtainConfigData = new ArrayList<>();
                mSceneConfigData = new ArrayList<>();
                mFloorHeatingConfigData = new ArrayList<>();
                for (PanelBindConfig config : result.getData().getPanelBindConfigs()){

                    if (config.getPanelIndex() == deviceCollectBean.getFromPos()){
                        if (config.getBindUIType() == 1){
                            mLightConfigData.add(config);
                        }else if (config.getBindUIType() == 2){
                            mAirConditionerConfigData.add(config);
                        }else if (config.getBindUIType() == 3){
                            mFreshAirConfigData.add(config);
                        }else if (config.getBindUIType() == 4){
                            mCurtainConfigData.add(config);
                        }else if (config.getBindUIType() == 5){
                            mSceneConfigData.add(config);
                        }else if (config.getBindUIType() == 6){
                            mFloorHeatingConfigData.add(config);
                        }
                    }
                }
                updateView();
            }
        });
    }

    private void updateView() {
        mSceneConfig.setRightText(mSceneConfigData.size() + "/" + SCENE_BIND_COUNT);
        mLightConfig.setRightText(mLightConfigData.size() + "/" + LIGHT_BIND_COUNT);
        mCurtainConfig.setRightText(mCurtainConfigData.size() + "/" + CURTAIN_BIND_COUNT);
        mAirConditionerType.setRightText(mAirConditionerConfigData.size() + "/" + AIR_CONDITIONER_COUNT);
        mFloorHeatingType.setRightText(mFloorHeatingConfigData.size() + "/" + FLOOR_HEATING_BIND_COUNT);
        mFreshAirConfig.setRightText(mFreshAirConfigData.size() + "/" + FRESH_AIR_BIND_COUNT);
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        Intent intent = new Intent(this,KGXPanelBindDeviceActivity.class);
        intent.putExtra("controllerSerialNo",deviceCollectBean.getControllerSerialNo());
        if (view == mSceneConfig){
            intent.putExtra("type","scene");
            intent.putExtra("count",SCENE_BIND_COUNT);
            intent.putExtra("bindUIType",5);
            intent.putExtra("bindType",7);
            intent.putExtra("data", (Serializable) mSceneConfigData);

        }else if (view == mLightConfig){

            intent.putExtra("type","light");
            intent.putExtra("count",LIGHT_BIND_COUNT);
            intent.putExtra("bindUIType",1);
            intent.putExtra("bindType",1);
            intent.putExtra("data", (Serializable) mLightConfigData);
        }else if (view == mCurtainConfig){

            intent.putExtra("type","curtain");
            intent.putExtra("count",CURTAIN_BIND_COUNT);
            intent.putExtra("bindUIType",4);
            intent.putExtra("bindType",5);
            intent.putExtra("data", (Serializable) mCurtainConfigData);
        }else if (view == mAirConditionerType){

            intent.putExtra("type","airconditioner");
            intent.putExtra("count",AIR_CONDITIONER_COUNT);
            intent.putExtra("bindUIType",2);
            intent.putExtra("bindType",3);
            intent.putExtra("fromPos",deviceCollectBean.getFromPos());
            intent.putExtra("data", (Serializable) mAirConditionerConfigData);
        }else if (view == mFreshAirConfig){

            intent.putExtra("type","freshair");
            intent.putExtra("count",FRESH_AIR_BIND_COUNT);
            intent.putExtra("bindUIType",3);
            intent.putExtra("bindType",4);
            intent.putExtra("fromPos",deviceCollectBean.getFromPos());
            intent.putExtra("data", (Serializable) mFreshAirConfigData );
        }else if (view == mFloorHeatingType){
            intent.putExtra("type","floorheating");
            intent.putExtra("count",FLOOR_HEATING_BIND_COUNT);
            intent.putExtra("bindUIType",6);
            intent.putExtra("bindType",8);
            intent.putExtra("fromPos",deviceCollectBean.getFromPos());
            intent.putExtra("data", (Serializable) mFloorHeatingConfigData);
        }
        startActivity(intent);
    }

    public static class UIPanelBindConfig{
        PanelBindConfig config;
        SceneBean sceneBean;
        DeviceBean deviceBean;
    }
}